package chucnang;

import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.*;

 

public class ComboxEditor extends DefaultCellEditor implements ItemListener {
protected JComboBox combox;
  private String    label;
  private boolean   isPushed;
  ResultSet rs;
  Statement st;
  Connection conn;
  private ChuyenKho luanchuyen;
  public ComboxEditor(ChuyenKho luanchuyen) {
	  super(new JCheckBox());
	  this.luanchuyen = luanchuyen; 
    
    combox = new JComboBox();   
    combox.setOpaque(true);
    combox.addItemListener(this);
    
    
    try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	
			conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=NhanVien","sa","123456");
	
			st = conn.createStatement();
		
		}catch (Exception ex) {
			ex.printStackTrace();
		}
		
     try {
	    	 rs = st.executeQuery("select * from HosoNV");	
	    	 while (rs.next()) {
	    		 
	    		//combox.addItem(rs.getString("Manv"));
	    		combox.addItem(rs.getString("HoTen"));
	    		//combox.addItem(rs.getString("NgaySinh"));
	    		
	    	 }
    	 }catch (Exception ex) {
    		 	ex.printStackTrace();

    	 }

    	
    
  }
  public Component getTableCellEditorComponent(JTable table, Object value,
                   boolean isSelected, int row, int column) {
    if (isSelected) {
    	combox.setForeground(table.getSelectionForeground());
    	combox.setBackground(table.getSelectionBackground());
    } else{
    	combox.setForeground(table.getForeground());
        combox.setBackground(table.getBackground());
    }
    label = (value ==null) ? "" : value.toString();
    combox.setToolTipText( label );
    isPushed = true;
    return combox;
  }
 
  public Object getCellEditorValue() {
    if (isPushed)  {
      // 
      // 
      //JOptionPane.showMessageDialog(combox ,label + ": Ouch!");
      // System.out.println(label + ": Ouch!");
    }
    isPushed = false;
    return new String( label ) ;
  }
   
  public boolean stopCellEditing() {
    isPushed = false;
    return super.stopCellEditing();
  }
 
  protected void fireEditingStopped() {
    super.fireEditingStopped();
  }


	@Override
	public void itemStateChanged(ItemEvent e) {
		if(e.getSource()==combox){
			fireEditingStopped();	
		      
//		    try {
//		       String sql = "select * from HosoNV WHERE HoTen = '" +combox.getSelectedItem().toString() + "'";
//		        rs = st.executeQuery(sql);
//		        System.out.println(combox.getSelectedItem().toString());
//		        if (rs.last()){
//		        	System.out.println(rs.getString(1));  
//		        	luanchuyen.table.setValueAt(rs.getString(1), luanchuyen.table.getSelectedRow(), 1);
//		        }
//		        else {
//		        	luanchuyen.table.getModel().setValueAt("", luanchuyen.table.getSelectedRow(), 1);
//		        }
//		    } catch (SQLException ex) {
//		       ex.printStackTrace();
//		    }
		}
	}
	
}